/*
 ----------------------------------------------------------------------------------
                           "THE BEER-WARE COPYRIGHT LICENSE"
                                     (Revision 43)
                                      -----------
    Rymek [rymek@rymek.org] wrote this file. As long as You retain this notice and
    this file has been sent directly to You by the author, You can do whatever You
    want with this stuff as long as You don't sell or rent it. If we meet some day,
    and You think this stuff is worth it, You can buy me a beer in return :-)
                                                                 __________
                                                                 Paweł Rymaszewski
  ----------------------------------------------------------------------------------
 */

package ui.windows.forms.searchForm;

import exceptions.NoResultsException;
import processing.logic.Fetcher;
import processing.logic.Search;
import processing.objects.Results;
import processing.objects.SearchParameters;
import processing.objects.Server;
import ui.messages.MessageBoxes;

import java.sql.SQLException;
import java.text.ParseException;
import java.util.Observable;
import java.util.Observer;

/**
 * Thread that fires up all search mechanisms.
 * Implemented to make UI responsive.
 *
 * @author rymek
 *         User: Rymek
 *         Date: 23.07.11
 *         Time: 22:32
 */
class SearchFormSearchThread extends Thread implements Observer {

    private final Search search;
    private final Fetcher fetcher;
    private final Results parsedResults;
    private final SearchParameters searchParameters;
    private final SearchForm searchForm;

    /**
     * Constructor.
     * Sets context to SearchForm object
     *
     * @param searchForm       parent search form
     * @param search           search object
     * @param server           Server object
     * @param parsedResults    parsed results object to modify
     * @param searchParameters search parameters
     */
    public SearchFormSearchThread(SearchForm searchForm, Search search, Server server, Results parsedResults, SearchParameters searchParameters) {

        this.searchForm = searchForm;
        this.search = search;
        this.fetcher = new Fetcher(server);
        this.parsedResults = parsedResults;
        this.searchParameters = searchParameters;
    }

    @Override
    public void run() {
        try {

            Results unparsedResults = this.search.findMessages(this.searchParameters);

            if (unparsedResults.getResultCount() == 0) try {
                throw new NoResultsException();

            } catch (NoResultsException error) {

                try {
                    this.searchForm.finishSearch();

                } catch (NoResultsException err) {
                    MessageBoxes.noResultsFoundException(err);
                }

                MessageBoxes.noResultsFoundException(error);
            }
            else {
                this.searchForm.setupProgressBarForFetch(unparsedResults.getResultCount());
                this.fetcher.addObserver(this);
                this.fetcher.fetchMessages(unparsedResults, this.parsedResults);
            }


        } catch (SQLException error) {
            MessageBoxes.sqlException(error);

        } catch (ParseException error) {
            MessageBoxes.genericErrorMessage(error);
        }
    }

    @Override
    /**
     * Updates progress bar in search form
     */
    public void update(Observable o, Object arg) {
        this.searchForm.updateProgresBarOnFetch();
    }

    /**
     * Terminates search process threads hence
     * cancelling search
     *
     * @throws SQLException thrown when Database Error occurs
     */
    public void terminateSearch() throws SQLException {
        this.fetcher.terminateThreads();
    }
}
